import json
import os
from django.db import models
from django.conf import settings

# 获取项目根目录路径
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 读取 config.json 文件获取部门信息
config_path = os.path.join(BASE_DIR, 'PM', 'config.json')
with open(config_path, 'r') as config_file:
    config = json.load(config_file)
    DEPARTMENTS = [(dept, dept) for dept in config.get('DEPARTMENTS', [])]


class ReportData(models.Model):
    data = models.JSONField(verbose_name="表单数据")  # 将表单数据存储为JSON格式

    before_clean_images = models.TextField(verbose_name="清洁前图片")
    after_clean_images = models.TextField(verbose_name="清洁后图片")

    report_date = models.DateField(verbose_name="报告日期")
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        on_delete=models.CASCADE, 
        verbose_name="创建用户", 
        related_name='reports',
        null=True
    )
    department = models.CharField(max_length=100, blank=True, null=True, verbose_name="部门")

    def __str__(self):
        return f"{self.data.get('customer_name', '')} - {self.report_date}"

    class Meta:
        verbose_name = "报告数据"
        verbose_name_plural = "报告数据"
